package com.smz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smz.entity.TCargo;
import com.smz.service.TCargoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
public class TCargoController {

    @Autowired
    private TCargoService tCargoService;



    @GetMapping("/findDyCargo/{id}")
    public ResponseEntity<List<TCargo>> findDyTCago(@PathVariable("id") Integer id) {
        List<TCargo> list = tCargoService.list(new QueryWrapper<TCargo>().lambda().eq(TCargo::getShelvesNo, id));
        return ResponseEntity.ok(list);
    }
    @ApiOperation("移货")
    @PostMapping("/moveGoods")
    public ResponseEntity<String> findByDy(@RequestBody TCargo tCargo) {
        System.out.println(tCargo.toString());
        String s = tCargoService.moveGoods(tCargo);
        return ResponseEntity.ok(s);
    }

    @ApiOperation("判断有无货")
    @GetMapping("/eqTcago/{id}")
    public ResponseEntity<String> findeqTcagoDy(@PathVariable("id")String id){
        TCargo one = tCargoService.getOne(new QueryWrapper<TCargo>().lambda().eq(TCargo::getCatgoNo, id));
        if(one.getStatus().equals("1")){
            return  ResponseEntity.ok("exit");
        }
        return ResponseEntity.ok("ok");

    }

    @ApiOperation("分页查询")
    @RequestMapping(value = "/tCargo/page",method = RequestMethod.GET)
    public ResponseEntity<Page<TCargo>> pageTCargo(@RequestParam(value = "current",defaultValue = "1")Integer current,@RequestParam(value = "rows",defaultValue = "1")Integer rows
                                                    ,@RequestParam(value = "tShelvesNo")Integer tShelvesNo,@RequestParam("damId") Integer damId
                                                   ,@RequestParam("wareId") Integer wareId){
        Page<TCargo> pageTCargo = tCargoService.findPageTCargo(current, rows,tShelvesNo,damId,wareId);
        return ResponseEntity.ok(pageTCargo);
    }

    @ApiOperation("分页搜索")
    @RequestMapping(value = "/tCargo/page/search",method = RequestMethod.GET)
    public ResponseEntity<Page<TCargo>> pageTCargoSearch(@RequestParam(value = "current",defaultValue = "1")Integer current,@RequestParam(value = "rows",defaultValue = "1")Integer rows
            ,@RequestParam(value = "shelvesNo")String shelvesNo,@RequestParam("CargoNo") String cargoNo
            ,@RequestParam("CargoStatus") String cargoStatus){
        Page<TCargo> pageTCargo = tCargoService.findPageTCargoSearch(current, rows,"".equals(shelvesNo)?null:Integer.valueOf(shelvesNo),"".equals(cargoNo)?null:Integer.valueOf(cargoNo),"".equals(cargoStatus)?null:Integer.valueOf(cargoStatus));
        return ResponseEntity.ok(pageTCargo);
    }

    @ApiOperation("添加仓位")
    @RequestMapping(value = "/tCargo",method = RequestMethod.POST)
    public String addTCargo(@RequestBody TCargo tCargo){
        tCargoService.save(tCargo);
        return "ok";
    }

    @ApiOperation("更新仓位")
    @RequestMapping(value = "/tCargo",method = RequestMethod.PUT)
    public String modifyTCargo(@RequestBody TCargo tCargo){
        tCargoService.update(tCargo,new QueryWrapper<TCargo>().lambda().eq(TCargo::getId,tCargo.getId()));
        return "ok";
    }

    /**
     * @Authoe: smz
     * @Description:   下拉框根据货架id查询下面所有货位
     * @Date: 10:57 2021-11-27
     */
    @GetMapping("findTCargosByShelvesId/{id}")
    public ResponseEntity<List<TCargo>> findTCargosByShelvesId(@PathVariable("id") Integer id){
        List<TCargo> list = tCargoService.list(new QueryWrapper<TCargo>().lambda().eq(TCargo::getShelvesNo, id));
        return ResponseEntity.ok(list);
    }

    /**
     * @Authoe: smz
     * @Description: 根据仓库，库区，货架，货位查询是否存在商品
     * @Date: 10:28 2021-11-30
     */

    @GetMapping("findTCargoByMenuIdAndMore/{menuId}/{reservoirId}/{ShelvesId}/{cargoId}")
    public ResponseEntity<TCargo> findTCargoByMenuIdAndMore(@PathVariable("menuId") String menuId,@PathVariable("reservoirId") String reservoirId,
                                                            @PathVariable("ShelvesId") String shelvesId,@PathVariable("cargoId") String cargoId){
        TCargo tCargo = tCargoService.findTCargoByMenuIdAndMore(menuId, reservoirId, shelvesId, cargoId);
        return ResponseEntity.ok(tCargo);
    }
}
